/**
 * @ClassName LC1848
 * @Author ykx
 * @Date 2024/7/5 22:11
 * @Version 1.0
 */
public class LC1848 {

    public static void main(String[] args) {
        LC1848 lc1848 = new LC1848();
        System.out.println(lc1848.getMinDistance(new int[]{5, 3, 6}, 5, 2));
    }

    public int getMinDistance(int[] nums, int target, int start) {
        if (nums[start] == target) {
            return 0;
        } else {
            int left = start - 1, right = start + 1;
            boolean rightExist = false, leftExist = false;
            for (int i = right; i < nums.length; i++) {
                if (nums[i] == target) {
                    rightExist = true;
                    right = i;
                    break;
                }
            }
            for (int i = left; i >= 0; i--) {
                if (nums[i] == target) {
                    leftExist = true;
                    left = i;
                    break;
                }
            }

            if (rightExist && !leftExist) {
                return right - start;
            }
            if (!rightExist && leftExist) {
                return start - left;
            }
            if (rightExist && leftExist) {
                return Math.min(Math.abs(start - left), Math.abs(start - right));
            }
            return -1;
        }
    }
}
